package com.realgodo.site.web.admin.news;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springside.modules.orm.Page;

import com.realgodo.site.entity.site.News;
import com.realgodo.site.service.site.NewsManager;
import com.realgodo.site.web.admin.AdminDefaultController;

@Scope("prototype")
@Results( { @Result(name = "toindex", type = "redirect", location = "${base}/admin/news/") })
public class IndexController extends AdminDefaultController {

	private static final long serialVersionUID = -3636496413757704331L;

	@Resource
	private NewsManager newsManager;

	private Page<News> newsPage = new Page<News>(PAGE_SIZE);

	private News news;

	private String title;

	private int status = -1;

	public String execute() {
		newsPage
				.setPageNo(newsManager.isParamNotNull(getParamMap(), Page.PAGE) ? Integer
						.valueOf((String) newsManager.getParamValue(
								getParamMap(), Page.PAGE))
						: 1);// 查询页数
		this.newsPage = this.newsManager.findNews(newsPage, getTitle(),
				getStatus(), false);
		super.setPageInfo(newsPage);
		return SUCCESS;
	}

	public String add() {
		if (null != getId()) {
			news = this.newsManager.get(getId());
		}
		return "add";
	}

	public String save() {
		this.newsManager.createNews(getNews());
		return "toindex";
	}

	public String front() {
		if (null != getId()) {
			this.newsManager.frontNews(getId());
		}
		return "toindex";
	}

	public String unfront() {
		if (null != getId()) {
			this.newsManager.unfrontNews(getId());
		}
		return "toindex";
	}

	public String remove() {
		if (null != getId()) {
			this.newsManager.delete(getId());
		}
		return "toindex";
	}

	/** --------------getter and setter---------------------- */
	public Page<News> getNewsPage() {
		return newsPage;
	}

	public void setNewsPage(Page<News> newsPage) {
		this.newsPage = newsPage;
	}

	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

}
